﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Alti.SharePoint.SPDataGenerator
{
    public static class Tools
    {
        public static string NormalizeAndPascaliseString(this string s)
        {
            s = s.Replace("_x0020_", "").Replace("_x0024_", "").Replace("_x003a_", "").Replace("_x00e9_", "");
            s = NormalizeString(s);
            s = PascalString(s);
            return s;
        }


        private static string NormalizeString(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                string normalizedString = s.Normalize(NormalizationForm.FormD);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < normalizedString.Length; i++)
                {
                    char c = normalizedString[i];
                    if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                        sb.Append(c);
                }
                return sb.ToString();
            }
            return string.Empty;
        }

        private static string PascalString(string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                char[] inputChars = input.ToLower().ToCharArray();
                StringBuilder sb = new StringBuilder();
                bool Maj = true;
                foreach (char aChar in inputChars)
                {
                    if ((aChar >= 'a' && aChar <= 'z') || (aChar >= '0' && aChar <= '9' && sb.Length > 0))
                    {
                        sb.Append((Maj ? aChar.ToString().ToUpper() : aChar.ToString()));
                        Maj = false;
                    }
                    else
                        Maj = true;
                }
                return sb.ToString();
            }
            return string.Empty;
        }

    }
}
